//
//  GYCity.m
//  GoodLuck
//
//  Created by apple on 2018/2/27.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYCity.h"
#import "GYNetWorkTools.h"

@implementation GYCity

+ (void)loadCity:(NSString *)area_id success:(void (^)(NSArray * _Nullable, NSError * _Nullable))finish {
    NSMutableArray *areas = [NSMutableArray array];
    
    //网络加载数据
    [EasyLodingView showLodingText:@"正在加载..." config:^EasyLodingConfig *{
        EasyLodingConfig *config = [EasyLodingConfig shared];
        config.showOnWindow = YES;
        config.superReceiveEvent = NO;
        
        return config;
    }];
    [GYNetWorkTools requestLocation:area_id success:^(NSDictionary *obj) {
        NSString *code = [obj objectForKey:@"code"];
        NSString *msg = [obj objectForKey:@"msg"];
        if ([code integerValue] == 200) {
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            //正确请求回来的数据
            NSDictionary *datas = [obj objectForKey:@"datas"];
            NSArray *area_list = [datas objectForKey:@"area_list"];
            for (NSDictionary *dic in area_list) {
                GYCity *area = [[GYCity alloc] initWithDic:dic];
                [areas addObject:area];
            }
            
            //通知界面 数据获取成功
            finish(areas,nil);
        }else {
            NSLog(@"网路加载数据出错===-%@",msg);
            [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
            [EasyTextView showText:@"加载数据出错,请重新加载!"];
        }
    } error:^(NSError *error) {
        [EasyLodingView hidenLoingInView:[UIApplication sharedApplication].keyWindow];
        [EasyTextView showText:@"加载数据出错,请重新加载!"];
        finish(nil,error);
    }];
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"key =====%@",key);
}

- (NSString *)description {
    NSArray *keys = @[@"area_id",@"area_name"];
    
    return [NSString stringWithFormat:@"%@",[self dictionaryWithValuesForKeys:keys]];
}

@end
